﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrm.Services.Realization;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace FangChanCrm.Controllers
{
    public class TuanDuiController : Controller
    {
        private readonly It_AnChangBiaoService t_AnChangBiaoService;
        private readonly It_TuanDuiBiaoService t_TuanDuiBiaoService;
        private readonly It_TuanDuiRenYuanGuanLianBiaoService t_TuanDuiRenYuanGuanLianBiaoService;
        private readonly It_TuanDuiAnChangGuanLianBiaoService t_TuanDuiAnChangGuanLianBiaoService;

        public TuanDuiController(It_AnChangBiaoService anChangBiaoService, It_TuanDuiBiaoService tuanDuiBiaoService, It_TuanDuiRenYuanGuanLianBiaoService tuanDuiRenYuanGuanLianBiaoService, It_TuanDuiAnChangGuanLianBiaoService tuanDuiAnChangGuanLianBiaoService, IConfiguration configuration)
        {
            t_AnChangBiaoService = anChangBiaoService;
            t_TuanDuiBiaoService = tuanDuiBiaoService;
            t_TuanDuiRenYuanGuanLianBiaoService = tuanDuiRenYuanGuanLianBiaoService;
            t_TuanDuiAnChangGuanLianBiaoService = tuanDuiAnChangGuanLianBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }




        public IActionResult TuanDuiAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult TuanDuiList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult TuanDuiPeiZhi()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public string TuanDuiJingLiShuJu() 
        {
            List<t_RenYuanBiao> ry = t_TuanDuiBiaoService.GetRenYuanJingLi(33);
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            for (int i = 0; i < ry.Count; i++)
            {
                t_RenYuanBiao ryxinxi = ry[i];
                Json.Append("{\"id\":\"" + ryxinxi.Id + "\",\"xingMing\":\"" + ryxinxi.XingMing + "\"}");
                if ((i + 1) == ry.Count)
                {

                }
                else
                {
                    Json.Append(",");
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

        public string BangDingZheYe() 
        {
            List<t_TuanDuiRenYuanGuanLianBiao> tuanduirenyuan = t_TuanDuiRenYuanGuanLianBiaoService.GetList(Convert.ToInt32(Request.Form["id"]));
            List<t_RenYuanBiao> ry = t_TuanDuiBiaoService.GetRenYuanJingLi(34);
            bool puanduandiyi = true;
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            for (int i = 0; i < ry.Count; i++)
            {
                bool panduan = true;
                for (int j = 0; j < tuanduirenyuan.Count; j++)
                {
                    if (ry[i].Id == tuanduirenyuan[j].ZhiYeGuWenId)
                    {
                        panduan = false;
                    }
                    else 
                    {
                    
                    }
                }

                if (panduan)
                {
                    if (puanduandiyi)
                    {
                        puanduandiyi = false;
                    }
                    else
                    {
                        Json.Append(",");
                    }
                }
                else
                {

                }

                if (panduan) 
                {
                    t_RenYuanBiao ryxinxi = ry[i];
                    Json.Append("{\"id\":\"" + ryxinxi.Id + "\",\"xingMing\":\"" + ryxinxi.XingMing + "\"}");
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

        public string BangDingAnChangShuJu()
        {
            List<t_TuanDuiAnChangGuanLianBiao> t_TuanDuiAnChangGuanLianBiao = t_TuanDuiAnChangGuanLianBiaoService.GetList(Convert.ToInt32(Request.Form["id"]));
            List<t_AnChangBiao> anchang = t_AnChangBiaoService.GetList();
            bool puanduandiyi = true;
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            for (int i = 0; i < anchang.Count; i++)
            {
                bool panduan = true;
                for (int j = 0; j < t_TuanDuiAnChangGuanLianBiao.Count; j++)
                {
                    if (anchang[i].Id == t_TuanDuiAnChangGuanLianBiao[j].AnChangId)
                    {
                        panduan = false;
                    }
                    else
                    {

                    }
                }

                if (panduan)
                {
                    if (puanduandiyi)
                    {
                        puanduandiyi = false;
                    }
                    else
                    {
                        Json.Append(",");
                    }
                }
                else
                {

                }

                if (panduan)
                {
                    t_AnChangBiao t_AnChangBiao = anchang[i];
                    Json.Append("{\"id\":\"" + t_AnChangBiao.Id + "\",\"xingMing\":\"" + t_AnChangBiao.AnChangMingCheng + "\"}");
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

        public string TuanDuiTianJia() 
        {
            string tuanduimingcheng = Request.Form["tuanduimingcheng"];
            string tuanduijingli = Request.Form["tuanduijingli"];
            t_TuanDuiBiao t_TuanDuiBiao = new t_TuanDuiBiao();
            t_TuanDuiBiao.TuanDuiMingCheng = tuanduimingcheng;
            t_TuanDuiBiao.JingLiId = Convert.ToInt32(tuanduijingli);
            t_TuanDuiBiao.ZhuangTai = "启用";
            int panduan = 0;
            panduan = t_TuanDuiBiaoService.Add(t_TuanDuiBiao);

            if (panduan > 0)
            {
                t_TuanDuiBiao tuandui = t_TuanDuiBiaoService.GetShuju(t_TuanDuiBiao);
                panduan = tuandui.Id;
            }
            else
            {
                panduan = 0;
            }
            return panduan.ToString();
        }

        public string TuanDuiRenYuanTianJia() 
        {
            string tuanduiid = Request.Form["tuanduiid"];
            string zhiyeguwen = Request.Form["zhiyeguwen"];
            t_TuanDuiRenYuanGuanLianBiao t_TuanDuiRenYuanGuanLianBiao = new t_TuanDuiRenYuanGuanLianBiao();
            t_TuanDuiRenYuanGuanLianBiao.ZhiYeGuWenId = Convert.ToInt32(zhiyeguwen);
            t_TuanDuiRenYuanGuanLianBiao.TuanDuiId = Convert.ToInt32(tuanduiid);
            int panduan = 0;
            panduan = t_TuanDuiRenYuanGuanLianBiaoService.Add(t_TuanDuiRenYuanGuanLianBiao);

            if (panduan > 0)
            {
                panduan = 1;
            }
            else
            {
                panduan = 0;
            }
            return panduan.ToString();
        }

        public string TianJIaAnChangShuJu()
        {
            string tuanduiid = Request.Form["tuanduiid"];
            string anchang = Request.Form["anchang"];
            t_TuanDuiAnChangGuanLianBiao t_TuanDuiAnChangGuanLianBiao = new t_TuanDuiAnChangGuanLianBiao();
            t_TuanDuiAnChangGuanLianBiao.AnChangId = Convert.ToInt32(anchang);
            t_TuanDuiAnChangGuanLianBiao.TuanDuiId = Convert.ToInt32(tuanduiid);
            int panduan = 0;
            panduan = t_TuanDuiAnChangGuanLianBiaoService.Add(t_TuanDuiAnChangGuanLianBiao);

            if (panduan > 0)
            {
                panduan = 1;
            }
            else
            {
                panduan = 0;
            }
            return panduan.ToString();
        }

        public string IniZheYeDataTable() 
        {
            int tuanduiid = Convert.ToInt32(Request.Form["tuanduiidzhi"]);
            return t_TuanDuiBiaoService.GetListWithFenYeZheYe(tuanduiid, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }

        public string IniAnChangDataTable() 
        {
            int tuanduiid = Convert.ToInt32(Request.Form["tuanduiidzhi"]);
            return t_TuanDuiBiaoService.GetListWithFenYeAnChang(tuanduiid, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }

        public string ZheYeShangChu() 
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            bool shu = t_TuanDuiBiaoService.DelZheYeShangChu(id);
            if (shu)
            {
                return "1";
            }
            else 
            {
                return "0";
            }
        }

        public string AnChangShangChu()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            bool shu = t_TuanDuiBiaoService.DelAnChangShangChu(id);
            if (shu)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string InitializationDataTable() 
        {
            t_TuanDuiBiao t_TuanDuiBiao = new t_TuanDuiBiao();
            t_TuanDuiBiao.TuanDuiMingCheng = Request.Form["tuanduimingcheng"].ToString();
            t_TuanDuiBiao.JingLiId = Convert.ToInt32(Request.Form["tuanduijingli"]);
            return t_TuanDuiBiaoService.GetListWithFenYe(t_TuanDuiBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }

        public string QiYong()
        {
            string id = Request.Form["id"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var t_TuanDuiBiao = _context.t_TuanDuiBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                t_TuanDuiBiao.ZhuangTai = "启用";
                panduan = _context.SaveChanges() > 0;
            }

            //t_TuanDuiBiao t_TuanDuiBiao = t_TuanDuiBiaoService.GetId(Convert.ToInt32(id));
            //t_TuanDuiBiao.ZhuangTai = "启用";
            //bool panduan = t_TuanDuiBiaoService.Update(t_TuanDuiBiao);
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string JinYong()
        {
            string id = Request.Form["id"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var t_TuanDuiBiao = _context.t_TuanDuiBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                t_TuanDuiBiao.ZhuangTai = "禁用";
                panduan = _context.SaveChanges() > 0;
            }

            //t_TuanDuiBiao t_TuanDuiBiao = t_TuanDuiBiaoService.GetId(Convert.ToInt32(id));
            //t_TuanDuiBiao.ZhuangTai = "禁用";
            //bool panduan = t_TuanDuiBiaoService.Update(t_TuanDuiBiao);
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string TuanDuiXgai() 
        {
            string id = Request.Form["id"];
            string tuanduimingcheng = Request.Form["tuanduimingcheng"];
            string tuanduijingli = Request.Form["tuanduijingli"];


            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var t_TuanDuiBiao = _context.t_TuanDuiBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                t_TuanDuiBiao.TuanDuiMingCheng = tuanduimingcheng;
                t_TuanDuiBiao.JingLiId = Convert.ToInt32(tuanduijingli);
                panduan = _context.SaveChanges() > 0;
            }

            //t_TuanDuiBiao t_TuanDuiBiao = t_TuanDuiBiaoService.GetId(Convert.ToInt32(id));
            //t_TuanDuiBiao.TuanDuiMingCheng = tuanduimingcheng;
            //t_TuanDuiBiao.JingLiId = Convert.ToInt32(tuanduijingli);
            //bool panduan = true;
            //panduan = t_TuanDuiBiaoService.Update(t_TuanDuiBiao);

            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string TuanDuiGetId() 
        {
            string id = Request.Form["id"];
            t_TuanDuiBiao t_TuanDuiBiao = t_TuanDuiBiaoService.GetId(Convert.ToInt32(id));
            string shuju = "{\"mingcheng\":\"" + t_TuanDuiBiao.TuanDuiMingCheng + "\",\"jingliid\":\"" + t_TuanDuiBiao.JingLiId + "\"}";
            return shuju;
        }

    }
}
